﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;

namespace $safeprojectname$
{
    public class RadioForNullableBool : PropertyControl<bool?, RadioForNullableBool>
    {
        #region members
        bool _hasDescription = true;
        #endregion members

        #region constructor
        public RadioForNullableBool() { }
        public RadioForNullableBool(bool? asReadonly = null) : base(asReadonly) { }
        public RadioForNullableBool(string prefix) : base(prefix) { }
        #endregion constructor

        #region OnSourceProperty
        protected override void OnSourcePropertyChanged(string propertyName, bool? value)
        {
            SetValue(value);
        }
        #endregion OnSourceProperty

        #region Render
        protected override void ReadControls()
        {
            CreateControls(true);
        }

        protected override void WriteControls()
        {
            CreateControls();
        }

        protected virtual void CreateControls(bool isDisabled = false)
        {
            Controls.Add(CreateSpan(Constants.WebControls.Yes, Special.True, Value.HasValue && Value.Value, isDisabled));
            Controls.Add(CreateSpan(Constants.WebControls.No, Special.False, Value.HasValue && !Value.Value, isDisabled));
            Controls.Add(CreateSpan(Constants.WebControls.Any, Special.Nothing, !Value.HasValue, isDisabled));
        }

        protected virtual Span CreateSpan(string label, string value, bool isChecked, bool isDisabled = false)
        {
            var span = new Span(label.Equals(Constants.WebControls.Any) ? " radio right " : " radio ");
            if (HasDescription)
            {
                span.AddControls(new Literal(label, true));
            }
            if (!HasHeadersOnly)
            {
                span.AddControls
                    (
                        new Input(InputType.Radio)
                            .SetName(InputName)
                            .SetValue(value)
                            .SetChecked(isChecked)
                            .SetDisabled(isDisabled)
                    );
            }
            return span;
        }
        #endregion Render

        #region Set()
        /// <summary>
        ///     Set the the current value, which will be rendered as:
        ///     Yes = true, No = false, No Matter = null
        /// </summary>
        /// <param name = "value"></param>
        /// <returns>this</returns>
        public virtual RadioForNullableBool SetValue(bool? value)
        {
            Value = value;
            return this;
        }

        /// <summary>
        ///     Dynamically allows to allow decorating the radio inputs with localized description
        /// </summary>
        /// <param name = "showDescription"></param>
        /// <returns>this</returns>
        public virtual RadioForNullableBool ShowDescription(bool showDescription = true)
        {
            HasDescription = showDescription;
            return this;
        }

        /// <summary>
        ///     Dynamically allows to switch this control to render only the Headers
        ///     enclosed in TagName dependent on RenderAsTableCells (SPAN or TD)
        /// </summary>
        /// <param name = "showHeadersOnly"></param>
        /// <returns>this</returns>
        public virtual RadioForNullableBool ShowHeadersOnly(bool showHeadersOnly)
        {
            HasHeadersOnly = showHeadersOnly;
            return this;
        }
        #endregion Set()

        #region properties
        /// <summary>
        ///     Esential property for Nullable bool Web control.
        ///     Default is null;
        /// </summary>
        public virtual bool? Value { get; set; }

        /// <summary>
        ///     Allows to render localized descriptions e.g.: Yes, No, NoMatter.
        ///     Default is false (no description)
        ///     Note: When HasHeadersOnly is set to true, this will return true anyway
        /// </summary>
        public virtual bool HasDescription
        {
            get { return _hasDescription || HasHeadersOnly; }
            set { _hasDescription = value; }
        }

        /// <summary>
        ///     This switch allows to use this control as the Header Render.
        ///     If true, then only Header descriptions are rendered inside the TagNames
        ///     (default is false)
        ///     Note: set to true will also set to true the HasDescription
        /// </summary>
        public virtual bool HasHeadersOnly { get; set; }
        #endregion properties
    }
}